package b4.view
{
	import as3isolib.geom.Pt;
	
	import b4.ApplicationFacade;
	import b4.config.Config;
	import b4.config.CreatureConfig;
	import b4.config.LayerConfig;
	import b4.config.LayerItemConfig;
	import b4.config.LayerItemConfigCreature;
	import b4.config.LayerItemConfigCreaturePos;
	import b4.config.LayerItemConfigPort;
	import b4.model.ResourceLoader;
	import b4.model.TownProxy;
	import b4.model.vo.player.Role;
	import b4.view.component.events.RoleEvent;
	import b4.view.component.town.RoleSprite;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	public class RoleSpriteMediator extends Mediator implements IMediator
	{
		public static const NAME:String = 'RoleSpriteMediator';
		public static const REMOVE:String = NAME + 'Remove';
		public static const SET_POS:String = NAME + 'SetPos';
		public static const SET_DEST:String = NAME + 'SetDestination';
		public static const POSITION_CHANGE:String = NAME + 'PostionChange';
		private var roleId:int;
		
		public function RoleSpriteMediator(roleId:int)
		{
			this.roleId = roleId;
			var roleSprite:RoleSprite = new RoleSprite();
			super(NAME + roleId, roleSprite);
		}
		
		override public function listNotificationInterests():Array
		{
			return [
				RoleSpriteMediator.SET_POS,
				RoleSpriteMediator.SET_DEST
			];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			var name:String = notification.getName();
			var body:Object = notification.getBody();
			var type:String = notification.getType();
			switch (name)
			{
				case RoleSpriteMediator.SET_POS:
					if (body.roleId == roleId)
					{
						roleSprite.x = body.posX;
						roleSprite.y = body.posY;
					}
				break;
				case RoleSpriteMediator.SET_DEST:
					if (body.roleId == roleId)
					{
						var layerConfig:LayerConfig = townProxy.vo.town.getCurrentLayerConfig();
						roleSprite.setDestination(layerConfig, body.posX, body.posY);
					}
				break;
				case RoleSpriteMediator.REMOVE:
					facade.removeMediator(this.getMediatorName());
				break;
			}
		}
		
		private function onReachLayerItem(event:RoleEvent):void
		{
			var currentLayerItem:LayerItemConfig = event.data as LayerItemConfig;
			if (currentLayerItem != null)
			{
				currentLayerItem.index
//				switch (currentLayerItem.type)
//				{
//					case LayerItemConfig.TYPE_PORT:
//						var layerItemPort:LayerItemConfigPort = currentLayerItem as LayerItemConfigPort;
//						sendNotification(ApplicationFacade.LOAD_TOWN_RESOURCE, {"type": ResourceLoader.TYPE_TOWN, "townIndex": layerItemPort.destTownIndex, "layerIndex": layerItemPort.destLayer});
//					break;
//					case LayerItemConfig.TYPE_CREATURE:
//						var layerItemCreature:LayerItemConfigCreature = currentLayerItem as LayerItemConfigCreature;
//						for (var posStr:String in layerItemCreature.creatures)
//						{
//							var layerItemCreaturePos:LayerItemConfigCreaturePos = layerItemCreature.creatures[posStr];
//							var creatureConfig:CreatureConfig = Config.objConfigs[layerItemCreaturePos.index] as CreatureConfig;
//						}
//					break;
//				}
			}
		}
		
		private function onPositionChange(event:RoleEvent):void
		{
			var pos:Pt = event.data as Pt;
			sendNotification(RoleSpriteMediator.POSITION_CHANGE, pos);
		}
		
		override public function onRegister():void
		{
			sendNotification(TownSceneMediator.ADD_CHILD, roleSprite);
			var role:Role = townProxy.vo.town.getRoleById(roleId);
			roleSprite.setAppearance(role.appearance);
			roleSprite.addEventListener(RoleEvent.ON_REACH_LAYER_ITEM, onReachLayerItem);
			roleSprite.addEventListener(RoleEvent.ON_POSITION_CHANGE, onPositionChange);
		}
		
		override public function onRemove():void
		{
			sendNotification(TownSceneMediator.REMOVE_CHILD, roleSprite);
		}
		
		public function get roleSprite():RoleSprite
		{
			return viewComponent as RoleSprite;
		}
		
		private function get townProxy():TownProxy
		{
			return facade.retrieveProxy( TownProxy.NAME ) as TownProxy;
		}
	}
}